package com.example.comm.Adapter;

import android.content.Context;
import android.content.Intent;
import android.os.Handler;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.example.comm.Bean.Community;
import com.example.comm.Bean.Post;
import com.example.comm.R;
import com.example.comm.activity.Receive;

import java.util.List;

public class ChatAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder>{
    private Context context;
    private List<Community> data;
    private final int N_TYPE=0;
    private final int F_TYPE=1;

    private int Max_num=15;
    private Boolean isfootview=true;//是否有footview

    public ChatAdapter(Context context,List<Community> data){
        this.context=context;
        this.data=data;
    }
    @NonNull
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view= LayoutInflater.from(parent.getContext()).inflate(R.layout.community_item,parent,false);
        View footview=LayoutInflater.from(view.getContext()).inflate(R.layout.foot_item,parent,false);
        if(viewType==F_TYPE){
            return new ChatAdapter.RecyclerViewHolder(footview,F_TYPE);
        }else{
            return new ChatAdapter.RecyclerViewHolder(view,N_TYPE);
        }
    }

    @Override
    public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, int position) {
        if(isfootview&& (getItemViewType(position))==F_TYPE){
            //底部加载提示
            final ChatAdapter.RecyclerViewHolder recyclerViewHolder=(ChatAdapter.RecyclerViewHolder)holder;
            recyclerViewHolder.Loading.setText("加载中...");
            Handler handler=new Handler();
            handler.postDelayed(new Runnable() {
                @Override
                public void run() {
                    Max_num+=8;
                    notifyDataSetChanged();
                }
            },2000);
        }else{
            final ChatAdapter.RecyclerViewHolder recyclerViewHolder=(ChatAdapter.RecyclerViewHolder)holder;
            final Community community=data.get(position);
            recyclerViewHolder.c_name.setText(community.getName());
            recyclerViewHolder.c_info.setText(community.getInfo());
            recyclerViewHolder.c_user.setText(community.getOnwer());

            recyclerViewHolder.itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    int position=recyclerViewHolder.getAdapterPosition();
                    Intent in=new Intent(context, Receive.class);
                    in.putExtra("c_name",community.getName());
                    in.putExtra("c_info",community.getInfo());
                    in.putExtra("c_user",community.getUser().getUsername());
                    in.putExtra("id",data.get(position).getObjectId());
                    context.startActivity(in);
                }
            });
        }
    }


    @Override
    public int getItemViewType(int position) {
        if(position==Max_num-1){//到了最底下
            return F_TYPE;
        }
        else return N_TYPE;
    }

    @Override
    public int getItemCount() {
        if(data.size()<Max_num){
            return  data.size();
        }
        return Max_num;
    }
    private class RecyclerViewHolder extends RecyclerView.ViewHolder {
        public TextView c_name,c_info,c_user;//ord_item的TextView
        public TextView Loading;

        public RecyclerViewHolder(View itemview, int view_type) {
            super(itemview);
            if(view_type==N_TYPE){
                c_name=itemview.findViewById(R.id.c_name);
                c_info=itemview.findViewById(R.id.c_info);
                c_user=itemview.findViewById(R.id.c_user);
            }else if(view_type==F_TYPE){
                Loading=itemview.findViewById(R.id.footText);
            }
        }
    }
}


